package com.czb.seckill.rabbitmq;

import cn.hutool.json.JSONUtil;
import com.czb.seckill.pojo.SeckillMessage;
import com.czb.seckill.pojo.User;
import com.czb.seckill.service.GoodsService;
import com.czb.seckill.service.OrderService;
import com.czb.seckill.vo.GoodsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author: 陈正彬
 * 消息的接收者
 */
@Service
@Slf4j
public class MQReceiverMessage {

    @Resource
    private GoodsService goodsService;
    @Resource
    private OrderService orderService;

    //方法 接收消息并完成下单
    @RabbitListener(queues = "seckillQueue")
    public void queue(String message){

        log.info("接收到的消息-->"+message);
        //转成SeckillMessage
        SeckillMessage seckillMessage = JSONUtil.toBean(message, SeckillMessage.class);

        User user = seckillMessage.getUser();
        Long goodsId = seckillMessage.getGoodsId();

        //提供商品id 获取对应的goodsVo
        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(goodsId);

        //下单
        orderService.seckill(user,goodsVo);


    }

}
